package com.qili.service.it.impl;

import com.qili.base.mapper.BaseMapper;
import com.qili.base.service.impl.BaseServiceImpl;
import com.qili.entity.supervisor.SupervisorFile;
import com.qili.mapper.it.ItDicMapper;
import com.qili.mapper.it.ItIndexMapper;
import com.qili.service.it.ItIndexService;
import com.qili.util.CommonUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.*;

@Service
public class ItIndexServiceImpl extends BaseServiceImpl<SupervisorFile,String> implements ItIndexService {

    @Autowired
    ItIndexMapper itIndexMapper;

    @Autowired
    ItDicMapper itDicMapper;


    @Override
    public BaseMapper<SupervisorFile, String> getMappser() {
        return itIndexMapper;

    }

    /**
     * 查询it知识库目录
     * @param map
     * @return
     */
    @Override
    public List<SupervisorFile> selectItknowledge(Map map){
       return itIndexMapper.selectItknowledge(map);
    }
    /**
     * 查询it知识库文件
     * @param map
     * @return
     */
    @Override
    public List<SupervisorFile> selectItFile(Map map){
        return itIndexMapper.selectItFile(map);
    }

    @Override
    public List<String> selectSeeAllRoles() {
        List<String> seeAllRoles = new ArrayList<>();
//        seeAllRoles.add("admin");
        return seeAllRoles;
    }

    @Override
    public List<String> selectDownloadAllRoles() {
        List<String> seeAllRoles = new ArrayList<>();
//        seeAllRoles.add("admin");
        return seeAllRoles;
    }

    /**
     * 判断当前用户所有角色中,是否有某一个拥有查看IT知识库全部文件夹的角色
     * @return
     */
    @Override
    public boolean canISeeAll(){
        List<String> userRoleList = CommonUtil.getUserRoleList();
        List<String> canSeeAllRoleList = selectSeeAllRoles();
        userRoleList.retainAll(canSeeAllRoleList);
        return !CollectionUtils.isEmpty(userRoleList);
    }

    @Override
    public boolean canIDownAll() {
        List<String> userRoleList = CommonUtil.getUserRoleList();
        //看看我是不是拥有所有文件下载权限的一员:
        List<String> canDownloadAllRoleList = selectDownloadAllRoles();
        userRoleList.retainAll(canDownloadAllRoleList);
        return !CollectionUtils.isEmpty(userRoleList);
    }

    @Override
    public boolean canIDownload(String dicId) {
        boolean canDownload = false;
        //看看我是不是拥有所有文件下载权限的一员:
        canDownload = canIDownAll();
        if(!canDownload){
            //判断用户角色是否拥有该文件夹下载权限:
            SupervisorFile supervisorFile = selectByPrimaryKey(dicId);
            String downloadUser = supervisorFile.getDownloadUser();
            if(StringUtils.isNotBlank(downloadUser)){
                //获取我所有的角色:
                List<String> userRoleList = CommonUtil.getUserRoleList();
                //获取该文件夹所有的可下载角色
                String[] split = downloadUser.split(",");
                List<String> canDownloadUsers = Arrays.asList(split);
                userRoleList.retainAll(canDownloadUsers);
                canDownload = !CollectionUtils.isEmpty(userRoleList);
            }
        }
        return canDownload;
    }

    @Override
    public List<SupervisorFile> selectThreeLevelFiles() {

        Map paraMap = new HashMap();
        List<String> userRoleList = CommonUtil.getUserRoleList();
        paraMap.put("examine",CommonUtil.getUserRoleList());
        // 2021 1207去掉 关于项目角色的判断
//        if( (userRoleList.contains("xmjl") || userRoleList.contains("jlgcs")) && !userRoleList.contains("zsk") ){
//            paraMap.put("project","1");
//            paraMap.put("userId",CommonUtil.getUserId());
//        }
        List<SupervisorFile> firstLevelFileList = itDicMapper.getFirst(paraMap);
        List<SupervisorFile> secondLevelFileList = itDicMapper.getSecond(paraMap);
        List<SupervisorFile> thirdLevelFileList = itDicMapper.getThird(paraMap);

        //存储一级Map   key:一级文件key      value: 一级对应的二级的集合
        Map<String,ArrayList<SupervisorFile>> firstLevelMap = new HashMap<String,ArrayList<SupervisorFile>>();

        //存储二级Map   key:二级文件的key    value: 二级对应的三级的集合
        Map<String,ArrayList<SupervisorFile>> secondLevelMap = new HashMap<String,ArrayList<SupervisorFile>>();

        //遍历第三级
        if(!CollectionUtils.isEmpty(thirdLevelFileList)){
            for(SupervisorFile thirdLevelFile : thirdLevelFileList){
                String myParentId = thirdLevelFile.getSuperiorId();
                if(StringUtils.isNotBlank(myParentId)){
                    ArrayList<SupervisorFile> brotherFiles = secondLevelMap.get(myParentId);
                    if(null == brotherFiles){
                        brotherFiles = new ArrayList<SupervisorFile>();
                    }
                    brotherFiles.add(thirdLevelFile);
                    secondLevelMap.put(myParentId,brotherFiles);
                }
            }
        }

        //遍历第二级
        if(!CollectionUtils.isEmpty(secondLevelFileList)){
            for(SupervisorFile secondLevelFile : secondLevelFileList){
                //去二级的map里面找一找有没有我的孩子的集合:
                ArrayList<SupervisorFile> supervisorFiles = secondLevelMap.get(secondLevelFile.getId());
                secondLevelFile.setChilds(supervisorFiles);
                String myParentId = secondLevelFile.getSuperiorId();
                if(StringUtils.isNotBlank(myParentId)){
                    ArrayList<SupervisorFile> brotherFiles = firstLevelMap.get(myParentId);
                    if(null == brotherFiles){
                        brotherFiles = new ArrayList<SupervisorFile>();
                    }
                    brotherFiles.add(secondLevelFile);
                    firstLevelMap.put(myParentId,brotherFiles);
                }
            }
        }

        //遍历第一级
        if(!CollectionUtils.isEmpty(firstLevelFileList)){
            for(SupervisorFile firstLevelFile : firstLevelFileList){
                //去一级map中找一找我的孩子:
                ArrayList<SupervisorFile> myChildList = firstLevelMap.get(firstLevelFile.getId());
                firstLevelFile.setChilds(myChildList);
            }
        }

        return firstLevelFileList;
    }

    /**
     * 获取所有的拥有所有文件下载权限的角色
     * @return
     */
    private List<String> getCanDownloadAllRoles(){
        ArrayList result = new ArrayList<String>();

        return result;
    }

}
